/*
* Copyright 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.factory.abstraction;

import com.laihuichuxing.factory.simple.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 *
 * @author: Qinxuefeng
 * @since: 1.0
 * Create Date Time: 2018/12/13 9:47
 * Update Date Time:
 */

public class OperationFactory {
    private static Map<String,Class<?>> allOperationMaps=new HashMap<>();
    private static void fillMap(){
        allOperationMaps.put("+", OperationAdd.class);
        allOperationMaps.put("-", OperationSub.class);
        allOperationMaps.put("*", OperationMul.class);
        allOperationMaps.put("/", OperationDiv.class);
    }
    public static Operation createOperation(String operator) throws IllegalAccessException, InstantiationException {
        Operation operation = null;
        fillMap();
        Class<?> clazz = allOperationMaps.get(operator);
        if(clazz==null){
            throw new RuntimeException("unsupported operation");
        }
        operation  =(Operation) clazz.newInstance();
        return operation;
    }
}
